import logging
import socket
import json
 
HOST = 'localhost'
PORT = 9999
ADDR = (HOST, PORT)
 
tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)
logging.info(f"connect success")
while True:
 
    data = input('> ')
    if not data:
        print("empty input data")
        break
    
    data = str.encode(data)
    # int 与 bytes 互转统一用大端模式
    tcpCliSock.send(int.to_bytes(len(data), 4, 'big') + data)
    length = int.from_bytes(tcpCliSock.recv(4), 'big')
    if not length:
        print("empty length")
        break
    logging.info(f"recv length:{length}")
    data = json.loads(tcpCliSock.recv(length))
    if not data:
        print("empty recv data")
        break
 
    print(data)